<?php

namespace app\controller;

use app\service\DtkService;
use app\BaseController;
use app\service\GoodsService;
use app\service\CategoryService;
use think\App;
use think\facade\View;

class Search extends BaseController
{
    protected $categoryService;
    protected $goodsService;
    protected $dtkService;
    private $searchPageSize = 20;

    /**
     * Category constructor.
     * @param App $app
     */
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->categoryService = app()->make(CategoryService::class);
        $this->goodsService = app()->make(GoodsService::class);
        $this->dtkService = app()->make(DtkService::class);
    }

    public function index()
    {
        $page = $this->request->get('p') ? $this->request->get('p') : 1;
        $keyword = $this->request->get('keyword') ? $this->request->get('keyword') : '';
        if (empty($keyword)) {
            return View::fetch('/error/index', array('msg' => '搜索关键字不能为空'));
        }
        //参数
        $params = array();
        $params['pageSize'] = $this->searchPageSize;
        $params['pageId'] = $page;
        $params['keyWords'] = $keyword;
        $searchData = $this->dtkService->search($params);
        if (empty($searchData)) {
            return View::fetch('/error/index', array('msg' => '没有找到相关商品'));
        }
        $maxPage = $searchData['maxPage'];
        $data['brandList'] = $this->goodsService->queryListByNum('brand_id > 0', 'commission_rate desc', 5);
        $data['list'] = $searchData['list'];
        $data['page'] = $page;
        $data['prevPage'] = $page >= 2 ? $page - 1 : 0;
        $data['nextPage'] = $page < $maxPage ? $page + 1 : 0;
        $data['keyword'] = $keyword;
        $data['maxPage'] = $maxPage;
        return View::fetch('/search/index', $data);
    }

    public function price() : string {
        $start = intval(request()->get('start'));
        $end = intval(request()->get('end'));
        $num = intval(request()->get('num'));
        if ($num <= 0) {
            $num = 20;
        }
        $detail['type'] = 'price';
        $detail['name'] = '低价好货';
        $data['detail'] = $detail;
        $page = $this->goodsService->queryListByPage('actual_price > '.$start.' and actual_price < '.$end, 'coupon_price desc',$num);
        $list = array();
        foreach ($page['data'] as $key=>$item) {
            if (empty($item['coupon_link'])) {
                continue;
            }
            $item['tkl'] = $this->createTkl($item['title'], $item['coupon_link'],$item['pic']);
            $list[$key] = $item;
        }
        $data['list'] = $list;
        return View::fetch('/goods/price', $data);
    }

    private function createTkl($title, $url, $pic) {
        $c = new \TopClient();
        $c->appkey = "27939665";
        $c->secretKey = "dec62626adcd2aa7290b82ed75144239";
        $c->format= 'json';
        $req = new \TbkTpwdCreateRequest();
        $req->setUrl($url);
        $req->setText($title);
        $req->setLogo($pic);
        $req->setExt("");
        $req->setUserId("");
        $res = $c->execute($req);
        if (!empty($res)) {
            // print_r($res);
            return $res->data->password_simple;
        }
    }
}
